/*
 * ImageLoader.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.yidiandao.business.R;

import java.io.File;

/**
 * 图片加载工具
 */
public class ImageLoader {

    public static void loadImage(Context context, final ImageView imageView, String url) {
        //
        //        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //
        //        try {
        //            if (context instanceof Activity && ((Activity) context).isFinishing()) {
        //                return;
        //            }
        //
        //            Glide.with(context)
        //                    .load(url)
        //                    .centerCrop()
        //                    .placeholder(R.mipmap.ic_image_normal_1)
        //                    .error(R.mipmap.ic_image_normal_1)
        //                    .priority(Priority.HIGH)
        //                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        //                    .listener(new RequestListener<String, GlideDrawable>() {
        //                        @Override
        //                        public boolean onException(Exception e, String model,
        //                                Target<GlideDrawable> target,
        //                                boolean isFirstResource) {
        //                            return false;
        //                        }
        //
        //                        @Override
        //                        public boolean onResourceReady(GlideDrawable resource, String model,
        //                                Target<GlideDrawable> target, boolean isFromMemoryCache,
        //                                boolean isFirstResource) {
        //                            imageView.setImageResource(0);
        //                            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //                            return false;
        //                        }
        //                    })
        //                    .into(imageView);
        //        } catch (Exception e) {
        //            e.printStackTrace();
        //        }

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .centerCrop()
                    .error(R.mipmap.ic_image_normal_1)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadImageWithRes(
            Context context, ImageView imageView, String url, int resId) {

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .centerCrop()
                    .error(resId)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载店铺logo
     */
    public static void loadShopLogo(Context context, ImageView imageView, String url) {

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .placeholder(R.mipmap.ic_shop_normal)
                    .centerCrop()
                    .error(R.mipmap.ic_shop_normal)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载店招图
     */
    public static void loadShopADImage(Context context, ImageView imageView, String url) {

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .centerCrop()
                    .error(R.mipmap.ic_image_normal_2_66)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadFromFile(Context context, ImageView imageView, File file) {
        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(file)
                    .centerCrop()
                    .error(R.mipmap.ic_image_normal_1)
                    .into(imageView);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadImageWithPlaceHolder(Context context, ImageView imageView, String url) {

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .placeholder(R.mipmap.ic_image_normal_1)
                    .centerCrop()
                    .error(R.mipmap.ic_image_normal_1)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载头像
     */
    public static void loadAvatar(final Context context, final ImageView imageView) {
        loadAvatar(context, imageView, PreferenceUtils.getSex(context));
    }

    public static void loadAvatar(final Context context, final ImageView imageView, String sex) {
        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            int res = "2".equals(sex)
                    ? R.mipmap.ic_avatar_women
                    : R.mipmap.ic_avatar_normal;
            Glide.with(context)
                    .load(res)
                    .placeholder(R.mipmap.ic_avatar_normal)
                    .centerCrop()
                    .error(R.mipmap.ic_avatar_normal)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载图片，带高斯模糊
     *
     * @param ivImage 加载正常图片的组件
     */
    public static void loadWithBlur(final Activity context, String url, final ImageView ivImage) {

        try {
            if (context.isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .asBitmap()
                    //                .placeholder(R.mipmap.ic_normal_image)
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(
                            new BitmapImageViewTarget(ivImage) {
                                @Override
                                protected void setResource(final Bitmap resource) {
                                    RoundedBitmapDrawable circularBitmapDrawable =
                                            RoundedBitmapDrawableFactory.create(
                                                    context.getResources(), resource);
                                    circularBitmapDrawable.setCircular(true);
                                    ivImage.setImageDrawable(circularBitmapDrawable);
                                    /*
                                    //高斯模糊
                                    new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            final Bitmap blur = BlurBitmap.blur(resource, 10,
                                            false);
                                            context.runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    ivBackground.setImageBitmap(blur);
                                                }
                                            });
                                        }
                                    }).start();*/
                                }
                            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载图片,图片完全显示前前所缩略图
     */
    public static void loadImageWithThumbnail(
            Context context, ImageView imageView, String url, int errorResId) {

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .centerCrop()
                    .thumbnail(0.3f)
                    .error(errorResId)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 具备优先级的图片加载方法
     */
    public static void loadBanner(Context context, final ImageView imageView, String url) {

        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .centerCrop()
                    .placeholder(R.mipmap.ic_image_normal_2_66)
                    .error(R.mipmap.ic_image_normal_2_66)
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model,
                                                   Target<GlideDrawable> target,
                                                   boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model,
                                                       Target<GlideDrawable> target, boolean isFromMemoryCache,
                                                       boolean isFirstResource) {
                            imageView.setImageResource(0);
                            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                            return false;
                        }
                    })
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载图片,图片完全显示前前所缩略图
     */
    public static void loadImageWithThumbnail(
            Context context, ImageView imageView, String url) {

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .centerCrop()
                    .thumbnail(0.3f)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadLocalImage(Context context, ImageView imageView, int imageResId) {

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(imageResId)
                    .fitCenter()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadImageFitXY(Context context, ImageView imageView, String url) {

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .fitCenter()
                    .error(R.mipmap.ic_image_normal_1)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadActiveImageFitXY(Context context, ImageView imageView, String url) {

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .fitCenter()
                    .placeholder(R.mipmap.ic_image_normal_2_66)
                    .error(R.mipmap.ic_image_normal_2_66)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void downloadImage(
            Context context, String url, final OnImageLoadListener listener) {

        try {
            if (context instanceof Activity && ((Activity) context).isFinishing()) {
                return;
            }

            Glide.with(context)
                    .load(url)
                    .asBitmap()
                    .into(
                            new SimpleTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(
                                        Bitmap resource,
                                        GlideAnimation<? super Bitmap> glideAnimation) {
                                    if (listener != null) {
                                        listener.onSuccess(resource);
                                    }
                                }
                            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface OnImageLoadListener {

        void onSuccess(Bitmap bitmap);
    }
}
